//定义全局变量
var $parent = $(".shopcart-item-box"); //购物车栏目区域
var $selectAll = $('.select-all');
var $curTotalEle = $(".total-price");


//计算价格
function calculate() {
	var num = 0,//商品数
		count = 0;//商品总数
	$(".merselect.selected").each(function() {
		var mernum = $(this).siblings('.cart-num-change').find('.cart-unit-num').val();
		num += (+mernum);
		var merpri = $(this).parent('.shopcart-item').find('.unit-price').text();
		count += mernum * merpri;
	});
	//console.log(count.toFixed(2));
	$curTotalEle.text(count.toFixed(2));
	var selectedItem = $parent.find('.merselect.selected');
	var itemsLen = $parent.children().length;
	//console.log(selectedItem.length + ":" + itemsLen);
	(selectedItem.length == itemsLen) && ($selectAll.addClass('selected'));
}

$(function() {

	//点击选择商品
	$parent.on('click', '.merselect', function() {
		var $that = $(this);

		if($that.hasClass('selected')) { //已选中该商品
			$that.removeClass('selected');
			$selectAll.removeClass('selected');

			//reduceMod($that, totalLast, identifier);
			//去掉全选选中状态
			$selectAll.removeClass('selected');

		} else {
			$that.addClass('selected');
			//plusMod($that, totalLast, identifier);
		}
		
		calculate();
	});
	//点击减少商品数量
	$parent.on('click', '.cart-reduce', function() {
		var $that = $(this);
		var curItemEle = $that.siblings('.cart-unit-num'),
			curItemNum = curItemEle.val();
		if(curItemNum <= 1) {
			curItemEle.val('1');
		} else {
			curItemNum--;
			curItemEle.val(curItemNum);
			var $ele = $that.parent().siblings('.merselect');
			calculate();
		}
	});
	//点击增加商品数量
	$parent.on('click', '.cart-add', function() {
		var $that = $(this);
		var curItemEle = $that.siblings('.cart-unit-num'),
			curItemNum = curItemEle.val();
		curItemNum++;
		curItemEle.val(curItemNum);
//		if($that.parent().siblings('.merselect').hasClass('selected')) {
//			var identifier = false;
//			var $ele = $that.parent().siblings('.merselect');
//			plusMod($ele, totalLast, identifier, unitItemNum);
//			unitItemNum = 0;
//		}
	calculate();

	});

	//全选事件处理
	var $itemsSelect = $('.shopcart-item-box').children().find('.merselect');
	var selet = true;
	$selectAll.click(function() {
		var $that = $(this);
		if(selet) {
			$that.addClass('selected');
			$itemsSelect.addClass('selected');
			//computeAll();
			calculate();
			selet = false;
		} else {
			selet = true;
			$that.removeClass('selected');
			$itemsSelect.removeClass('selected');
			$curTotalEle.text('0.00');
		}
	});

	//点击编辑事件处理
	var $editBtn = $('.edit-btn'),
		$total = $('.total-area'),
		$settleBtn = $('.settle-btn');
	$delBtn = $('.del-btn');
	var editSwitch = true;
	$editBtn.click(function() {
		if(editSwitch) {
			$(this).text('完成');
			$total.addClass('hide');
			$settleBtn.addClass('hide');
			$delBtn.removeClass('hide');
			editSwitch = false;
		} else {
			$(this).text('编辑');
			$total.removeClass('hide');
			$settleBtn.removeClass('hide');
			$delBtn.addClass('hide');
			$selectAll.removeClass('selected');
			$itemsSelect.removeClass('selected');
			$curTotalEle.text('0.00');
			selet = true;
			editSwitch = true;
		}
	});

	//删除商品
	$delBtn.click(function() {
		var $ele = $(".shopcart-item-box .shopcart-item .selected");
		console.log($ele.length);
		if($ele.length == 0) {
			YDUI.dialog.alert('请至少选择一项商品！');
		} else {
			YDUI.dialog.confirm('删除商品提示', '请确认是否删除该商品！', function() {
				cartOperate($ele, true);
			});
		}
	});

	//结算商品
	var cartIds=[];
	$settleBtn.click(function() {
		var $selectitem = $('.shopcart-item .merselect.selected');
		if($selectitem.length == 0) {
			YDUI.dialog.alert('请至少选择一项商品！');
		} else {
			$selectitem.each(function() {
				var cartid=$(this).parent('.shopcart-item').data('cartid');
				cartIds.push(cartid);
			});
			console.log(cartIds);
			cartOperate($selectitem, false);
		}

	});

	function cartOperate($ele, delJudge) { //结算或删除操作
		if(delJudge) {
			//删除操作
			$.ajax({
				type: "post",
				url: "",
				success: function() {

				}
			});
			$ele.parent('.shopcart-item').remove();
			$selectAll.removeClass('selected');
			checkCartNum();
		} else {
			//结算
			YDUI.dialog.loading.open('正在为您结算');
			$.ajax({
				type: 'post',
				url: "",
				success: function() {
					window.location.href='confirm-order.html';
				},
				complete: function() {
					//YDUI.dialog.loading.close();
					window.location.href='confirm-order.html';
				}
			})
		}

	}
	
	checkCartNum();
	function checkCartNum(){
		var carnumbers=$(".shopcart-item-box .shopcart-item").length;
		var str=`
				<div class="empty-carts">
					<img src="images/empty-cart.png"/>
					<p>购物车是空的</p>
					<a href="" class="button-yellow button-confirm">去逛逛</a>
				</div>
		`
		if(carnumbers==0){
			$parent.html(str);
		}
	}

})